package d_用户信息.b_网络.a_获取用户encrytKey;

import a_接口调用凭证.a_获取接口调用凭据.GetAccessToken;
import c_小程序登录.a_小程序登录.Code2Session;
import com.fasterxml.jackson.annotation.JsonProperty;
import my.wechat.rc.common.HttpClientUtil;
import my.wechat.rc.common.MapUtil;
import my.wechat.rc.common.SHAUtil;

import java.util.List;

/**
 * @author 86153
 * @date 2022/8/3
 **/
public class GetUserEncryptKey {

    public static GetUserEncryptKeyResponse getUserEncryptKey(String jsCode) {
        try {
            String accessToken = GetAccessToken.getAccessToken().getAccessToken();
            Code2Session.Code2SessionResponse code2Session = Code2Session.code2Session(jsCode);
            String url = "https://api.weixin.qq.com/wxa/business/getuserencryptkey?access_token=" + accessToken;
            return HttpClientUtil.get(url, MapUtil.of("openid", code2Session.getOpenid(),
                    "signature", SHAUtil.hmacSha256("", code2Session.getSessionKey()),
                    "sig_method", "hmac_sha256"),
                    GetUserEncryptKeyResponse.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new GetUserEncryptKeyResponse();
    }

    public static GetUserEncryptKeyResponse getUserEncryptKey() {
        //jscode 通过Code2Session中小程序开发工具，小程序登录获取code
        return getUserEncryptKey("033ueUFa1ZTfDD09M9Ha13bo2O0ueUFe");
    }

    public static void main(String[] args) {
        getUserEncryptKey();
    }

    public static class GetUserEncryptKeyResponse {
        private Integer errcode;
        private String errmsg;
        @JsonProperty("key_info_list")
        private List<KeyInfo> keyInfoList;//用户最近三次的加密 key 列表

        public Integer getErrcode() {
            return errcode;
        }

        public void setErrcode(Integer errcode) {
            this.errcode = errcode;
        }

        public String getErrmsg() {
            return errmsg;
        }

        public void setErrmsg(String errmsg) {
            this.errmsg = errmsg;
        }

        public List<KeyInfo> getKeyInfoList() {
            return keyInfoList;
        }

        public void setKeyInfoList(List<KeyInfo> keyInfoList) {
            this.keyInfoList = keyInfoList;
        }

        public static class KeyInfo {
            @JsonProperty("encrypt_key")
            private String encryptKey;//加密key
            private Integer version;//key的版本号
            @JsonProperty("expire_in")
            private Integer expireIn;//剩余有效时间
            private String iv;//加密iv
            @JsonProperty("create_time")
            private Integer createTime;//创建 key 的时间戳

            public String getEncryptKey() {
                return encryptKey;
            }

            public void setEncryptKey(String encryptKey) {
                this.encryptKey = encryptKey;
            }

            public Integer getVersion() {
                return version;
            }

            public void setVersion(Integer version) {
                this.version = version;
            }

            public Integer getExpireIn() {
                return expireIn;
            }

            public void setExpireIn(Integer expireIn) {
                this.expireIn = expireIn;
            }

            public String getIv() {
                return iv;
            }

            public void setIv(String iv) {
                this.iv = iv;
            }

            public Integer getCreateTime() {
                return createTime;
            }

            public void setCreateTime(Integer createTime) {
                this.createTime = createTime;
            }
        }
    }

}
